package com.photoeditor.demo.ui.dialog;

import android.support.annotation.IntDef;
import android.text.TextUtils;

import com.photoeditor.demo.model.preference.SpUtils;
import com.photoeditor.demo.statistics.StatisticsConstant;
import com.photoeditor.demo.statistics.StatisticsUtils;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import static com.photoeditor.demo.model.preference.SpUtils.EFFECT_NAME;
import static com.photoeditor.demo.model.preference.SpUtils.ISFROMBUTTON;

public class DialogStatistic {

    @IntDef(value = {
            RateWhere.TYPE_DEFAULT,
            RateWhere.TYPE_ID_FACE_AGING,
            RateWhere.TYPE_ID_GENDER_SWAP,
            RateWhere.TYPE_ID_FACE_SWAP,
            RateWhere.TYPE_ID_PAST_LIFE,
            RateWhere.TYPE_ID_EDIT,
            RateWhere.TYPE_ID_FILTER,
            RateWhere.TYPE_ID_MIRROR,
            RateWhere.TYPE_ID_WATERMARK,
            RateWhere.TYPE_ID_BEAUTY,
            RateWhere.TYPE_ID_TEXT,
            RateWhere.TYPE_ID_MIXER,
            RateWhere.TYPE_ID_BLUR,
            RateWhere.TYPE_ID_ADJUST,
            RateWhere.TYPE_ID_CROP,
            RateWhere.TYPE_ID_ROTATE
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface RateWhere {
        int TYPE_DEFAULT = 0;
        int TYPE_ID_FACE_AGING = 2;
        int TYPE_ID_GENDER_SWAP = 3;
        int TYPE_ID_FACE_SWAP = 4;
        int TYPE_ID_PAST_LIFE = 5;
        int TYPE_ID_FUTURE_BABY = 6;
        int TYPE_ID_EDIT = 1;
        int TYPE_ID_FILTER = 10;
        int TYPE_ID_MIRROR= 11;
        int TYPE_ID_WATERMARK = 12;
        int TYPE_ID_BEAUTY = 13;
        int TYPE_ID_TEXT = 14;
        int TYPE_ID_MIXER = 15;
        int TYPE_ID_BLUR = 16;
        int TYPE_ID_ADJUST = 17;
        int TYPE_ID_CROP = 18;
        int TYPE_ID_ROTATE = 19;
    }

    private static String rateFromWhere(@RateWhere int where) {
        String value = null;
        switch (where) {
            case RateWhere.TYPE_DEFAULT:
                value = StatisticsConstant.VALUE_RATE_TIME_SAVING;
                break;
            case RateWhere.TYPE_ID_EDIT:
                value = StatisticsConstant.VALUE_RATE_TIME_EDIT;
                break;
            case RateWhere.TYPE_ID_FACE_AGING:
                value = StatisticsConstant.VALUE_RATE_TIME_FACE_AGING;
                break;
            case RateWhere.TYPE_ID_GENDER_SWAP:
                value = StatisticsConstant.VALUE_RATE_TIME_GENDER_SWAP;
                break;
            case RateWhere.TYPE_ID_FACE_SWAP:
                value = StatisticsConstant.VALUE_RATE_TIME_FACE_SWAP;
                break;
            case RateWhere.TYPE_ID_PAST_LIFE:
                value = StatisticsConstant.VALUE_RATE_TIME_PAST_LIFE;
                break;

            case RateWhere.TYPE_ID_FILTER:
                value = "filter";
                break;
            case RateWhere.TYPE_ID_MIRROR:
                value = "mirror";
                break;
            case RateWhere.TYPE_ID_WATERMARK:
                value = "watermark";
                break;
            case RateWhere.TYPE_ID_BEAUTY:
                value = "beauty";
                break;
            case RateWhere.TYPE_ID_TEXT:
                value = "text";
                break;
            case RateWhere.TYPE_ID_MIXER:
                value = "mixer";
                break;
            case RateWhere.TYPE_ID_BLUR:
                value = "blur";
                break;
            case RateWhere.TYPE_ID_ADJUST:
                value = "adjust";
                break;
            case RateWhere.TYPE_ID_CROP:
                value = "crop";
                break;
            case RateWhere.TYPE_ID_ROTATE:
                value = "rotate";
                break;
        }
        return value;
    }

    /**
     * 评分引导弹窗展示统计
     *
     * @param where 0-saving
     *              1-Face Aging
     */
    public static void rateShow(@RateWhere int where) {
        String value = rateFromWhere(where);
        if (TextUtils.isEmpty(value)) {
            return;
        }
        StatisticsUtils.statisticGrowingIo(
                StatisticsConstant.EVENT_RATE_POP_SHOW,
                StatisticsConstant.KEY_RATE_TIME,
                value);
    }

    public static void rateBadShow(@RateWhere int where) {
        String value = rateFromWhere(where);
        if (TextUtils.isEmpty(value)) {
            return;
        }
        StatisticsUtils.statisticGrowingIo(
                StatisticsConstant.EVENT_BAD_REASON_SHOW,
                StatisticsConstant.KEY_RATE_TIME,
                value);
    }


    public static void rateGoodShow(@RateWhere int where) {
        String value = rateFromWhere(where);
        if (TextUtils.isEmpty(value)) {
            return;
        }
        StatisticsUtils.statisticGrowingIo(
                StatisticsConstant.EVENT_GOOD_SHOW,
                StatisticsConstant.KEY_RATE_TIME,
                value);
    }

    /**
     * 评分引导弹窗关闭统计
     *
     * @param where 0-saving
     *              1-Face Aging
     */
    public static void rateClose(int where) {
        String value = rateFromWhere(where);
        if (TextUtils.isEmpty(value)) {
            return;
        }
        StatisticsUtils.statisticGrowingIo(
                StatisticsConstant.EVENT_RATE_POP_CLOSE,
                StatisticsConstant.KEY_RATE_TIME,
                value);
    }

    public static void rateBadClose(int where) {
        String value = rateFromWhere(where);
        if (TextUtils.isEmpty(value)) {
            return;
        }
        StatisticsUtils.statisticGrowingIo(
                StatisticsConstant.EVENT_BAD_REASON_CLOSE,
                StatisticsConstant.KEY_RATE_TIME,
                value);
    }

    public static void rateGoodClose(int where) {
        String value = rateFromWhere(where);
        if (TextUtils.isEmpty(value)) {
            return;
        }
        StatisticsUtils.statisticGrowingIo(
                StatisticsConstant.EVENT_GOOD_CLOSE,
                StatisticsConstant.KEY_RATE_TIME,
                value);
    }

    /**
     * 评分引导弹窗好评统计
     *
     * @param where 0-saving
     *              1-Face Aging
     */
    public static void rateGoodComm(int where) {
        String value = rateFromWhere(where);
        if (TextUtils.isEmpty(value)) {
            return;
        }
        StatisticsUtils.statisticGrowingIo(
                StatisticsConstant.EVENT_GOOD_COMM,
                StatisticsConstant.KEY_RATE_TIME,
                value);
    }

    /**
     * 评分引导弹窗差评统计
     *
     * @param where 0-saving
     *              1-Face Aging
     */
    public static void rateBadComm(int where) {
        String value = rateFromWhere(where);
        if (TextUtils.isEmpty(value)) {
            return;
        }
        StatisticsUtils.statisticGrowingIo(
                StatisticsConstant.EVENT_BAD_COMM,
                StatisticsConstant.KEY_RATE_TIME,
                value);
    }

    public static void rateBadSub(int where, String reason) {
        String value = rateFromWhere(where);
        if (TextUtils.isEmpty(value)) {
            return;
        }
        StatisticsUtils.statisticGrowingIo(
                StatisticsConstant.EVENT_BAD_REASON_SUB,
                StatisticsConstant.KEY_RATE_TIME,
                value,
                StatisticsConstant.KEY_RATE_BAD_REASON,
                reason);
    }

    public static void rateGoodSub(int where) {
        String value = rateFromWhere(where);
        if (TextUtils.isEmpty(value)) {
            return;
        }
        StatisticsUtils.statisticGrowingIo(
                StatisticsConstant.EVENT_GOOD_RATE,
                StatisticsConstant.KEY_RATE_TIME,
                value);
    }

    @IntDef(value = {
            AdVideoWhere.VALUE_WHERE_MATERIAL,
            AdVideoWhere.VALUE_WHERE_TOOL,
            AdVideoWhere.VALUE_WHERE_FACE_AGING,
            AdVideoWhere.VALUE_WHERE_GENDER_SWAP,
            AdVideoWhere.VALUE_WHERE_FACE_SWAP,
            AdVideoWhere.VALUE_WHERE_PAST_LIFE,
            AdVideoWhere.VALUE_WHERE_FUTURE_BABY,
            AdVideoWhere.VALUE_WHERE_STICKERS

    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface AdVideoWhere {
        int VALUE_WHERE_MATERIAL = 0;
        int VALUE_WHERE_TOOL = 1;
        int VALUE_WHERE_FACE_AGING = 2;
        int VALUE_WHERE_GENDER_SWAP = 3;
        int VALUE_WHERE_FACE_SWAP = 4;
        int VALUE_WHERE_PAST_LIFE = 5;
        int VALUE_WHERE_FUTURE_BABY = 6;
        int VALUE_WHERE_STICKERS = 7;

    }


    public static String watchAdVideoFromWhere(@AdVideoWhere int where) {
        String value = null;
        switch (where) {
            case AdVideoWhere.VALUE_WHERE_MATERIAL:
                value = StatisticsConstant.VALUE_VIP_FUN_MATERIAL;
                break;
            case AdVideoWhere.VALUE_WHERE_TOOL:
                value = StatisticsConstant.VALUE_VIP_FUN_TOOL;
                break;
            case AdVideoWhere.VALUE_WHERE_FACE_AGING:
                value = StatisticsConstant.VALUE_VIP_FUN_FACE_AGING;
                break;
            case AdVideoWhere.VALUE_WHERE_GENDER_SWAP:
                value = StatisticsConstant.VALUE_VIP_FUN_GENDER_SWAP;
                break;
            case AdVideoWhere.VALUE_WHERE_FACE_SWAP:
                value = StatisticsConstant.VALUE_VIP_FUN_FACE_SWAP;
                break;
            case AdVideoWhere.VALUE_WHERE_PAST_LIFE:
                value = StatisticsConstant.VALUE_VIP_FUN_PAST_LIFE;
                break;
            case AdVideoWhere.VALUE_WHERE_FUTURE_BABY:
                value = StatisticsConstant.VALUE_VIP_FUN_FUTURE_BABY;
                break;
            case AdVideoWhere.VALUE_WHERE_STICKERS:
                value = StatisticsConstant.VALUE_VIP_FUN_STICKER;
                break;
        }
        return value;
    }

    /**
     * 激励视频展示统计
     *
     * @param where 0-vipMaterial
     *              1-vipTool
     *              2-Face Aging
     *              3-genderSwap
     *              4-faceSwap
     */
    public static void adVideoShow(@AdVideoWhere int where) {
        String value = watchAdVideoFromWhere(where);
        if (TextUtils.isEmpty(value)) {
            return;
        }
        StatisticsUtils.statisticGrowingIo(
                StatisticsConstant.EVENT_VIDEO_POP_SHOW,
                StatisticsConstant.KEY_VIP_FUN,
                value);
    }

    /**
     * 激励视频关闭统计
     *
     * @param where 0-vipMaterial
     *              1-vipTool
     *              2-Face Aging
     *              3-genderSwap
     *              4-faceSwap
     */
    public static void adVideoClose(int where) {
        String value = watchAdVideoFromWhere(where);
        if (TextUtils.isEmpty(value)) {
            return;
        }
        StatisticsUtils.statisticGrowingIo(
                StatisticsConstant.EVENT_VIDEO_POP_CLOSE,
                StatisticsConstant.KEY_VIP_FUN,
                value);
    }

    /**
     * 激励视频确认按钮点击统计
     *
     * @param where 0-vipMaterial
     *              1-vipTool
     *              2-Face Aging
     *              3-genderSwap
     *              4-faceSwap
     */
    public static void adVideoSureClick(int where) {
        String value = watchAdVideoFromWhere(where);
        if (TextUtils.isEmpty(value)) {
            return;
        }

       /* StatisticsUtils.statisticGrowingIo(StatisticsConstant.EVENT_VIDEO_BUTTON_CLICK,
                StatisticsConstant.KEY_SHOW_TYPE, StatisticsConstant.VALUE_POPUP,
                StatisticsConstant.KEY_VIP_FUN, value);*/
    }

    /**
     * 激励视频取消按钮点击统计
     *
     * @param where 0-vipMaterial
     *              1-vipTool
     *              2-Face Aging
     *              3-genderSwap
     *              4-faceSwap
     */
    public static void adVideoCancelClick(int where) {
        String value = watchAdVideoFromWhere(where);
        if (TextUtils.isEmpty(value)) {
            return;
        }
        StatisticsUtils.statisticGrowingIo(
                StatisticsConstant.EVENT_VIDEO_POP_NO,
                StatisticsConstant.KEY_VIP_FUN,
                value);
    }

    /**
     * 激励视频解锁单次功能统计
     *
     * @param where 0-vipMaterial
     *              1-vipTool
     *              2-Face Aging
     *              3-genderSwap
     *              4-faceSwap
     */
    public static void adVideoUnlock(int where) {
        String value = watchAdVideoFromWhere(where);
        if (TextUtils.isEmpty(value)) {
            return;
        }

        if(SpUtils.obtain().getBoolean(ISFROMBUTTON, false)){
            StatisticsUtils.statisticGrowingIo(
                    StatisticsConstant.EVENT_VIDEO_POP_UNLOCK,
                    StatisticsConstant.KEY_VIP_FUN,
                    value,
                    StatisticsConstant.KEY_SHOW_TYPE,
                    StatisticsConstant.VALUE_BUTTON,
                    StatisticsConstant.KEY_FILTER_NAME,
                    SpUtils.obtain().getString(EFFECT_NAME, "")
            );
        }else{
            StatisticsUtils.statisticGrowingIo(
                    StatisticsConstant.EVENT_VIDEO_POP_UNLOCK,
                    StatisticsConstant.KEY_VIP_FUN,
                    value,
                    StatisticsConstant.KEY_SHOW_TYPE,
                    StatisticsConstant.VALUE_POPUP,
                    StatisticsConstant.KEY_FILTER_NAME,
                    SpUtils.obtain().getString(EFFECT_NAME, "")
            );
        }

        SpUtils.obtain().save(ISFROMBUTTON, false);
    }
}
